import { NormalActionResponse,ApiQueryResponse } from "./vo";
import {fileFolderList} from "./vo";
import ajax from "../../../modules/ajax";
import config from "../../../config.json";

export const apiGet = {
    getApiInformationById: async (id:number):Promise<ApiQueryResponse> => {
        let res = await ajax.ajax(config.requestUrl + "/gapi/api/get",{
            method: "GET",
            body: {apiId: id}
        });
        if(res.ok) {
            return res.json();
        } else {
            return Promise.reject("网络问题");
        }
    }
}

export const apiMod = {
    delete: async (id:number):Promise<NormalActionResponse> => {
        let res = await ajax.ajax(config.requestUrl + "/gapi/api/delete",{
            method: "POST",
            body: JSON.stringify({apiId: id}),
            headers: {
                "Content-Type" : "application/json",
            },
        });
        if(res.ok) {
            return res.json();
        } else {
            return Promise.reject("网络问题");
        }
    },
    update: async (mod:any):Promise<NormalActionResponse> => {
        let res = await ajax.ajax(config.requestUrl + "/gapi/api/update",{
            method: "POST",
            body: JSON.stringify(mod),
            headers: {
                "Content-Type" : "application/json",
            },
        });
        if(res.ok) {
            return res.json();
        } else {
            return Promise.reject(res.text);
        }
    },
    create: async (body:any):Promise<NormalActionResponse> => {
        let res = await ajax.ajax(config.requestUrl + "/gapi/api/create",{
            method: "POST",
            body: JSON.stringify(body),
            headers: {
                "Content-Type" : "application/json",
            },
        });
        if(res.ok) {
            return res.json();
        } else {
            return Promise.reject("网络问题");
        }
    }
}

export const group = {
    query: async (id:string):Promise<fileFolderList> => {
        let res = await ajax.ajax(config.requestUrl + "/gapi/folder/getList",{
            method: "GET",
            body: {projectId: id},
        });
        if(res.ok) {
            return res.json();
        } else {
            return Promise.reject({errCode: res.statusCode, msg: res.text});
        }
    },
    create: async (body: any):Promise<NormalActionResponse> => {
        let res = await ajax.ajax(config.requestUrl + "/gapi/folder/create",{
            method: "POST",
            body: JSON.stringify(body),
            headers: {
                "Content-Type" : "application/json",
            },
        });
        if(res.ok) {
            return res.json();
        } else {
            return Promise.reject("网络问题");
        }
    },
    delete: async (id: number):Promise<NormalActionResponse> => {
        let res = await ajax.ajax(config.requestUrl + "/gapi/folder/delete",{
            method: "POST",
            body: JSON.stringify({folderId: id}),
            headers: {
                "Content-Type" : "application/json",
            },
        });
        if(res.ok) {
            return res.json();
        } else {
            return Promise.reject("网络问题");
        }
    },
    rename: async (body: any):Promise<NormalActionResponse> => {
        let res = await ajax.ajax(config.requestUrl + "/gapi/folder/rename",{
            method: "POST",
            body: JSON.stringify(body),
            headers: {
                "Content-Type" : "application/json",
            },
        });
        if(res.ok) {
            return res.json();
        } else {
            return Promise.reject("网络问题");
        }
    },
}
